/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginDlg.java
 *
 * Created on Jul 10, 2011, 10:36:17 AM
 */
package com.viweb.storemanager.ui;

import com.viweb.storemanager.core.CursorManager;
import com.viweb.storemanager.core.OptionPane;
import com.viweb.storemanager.dao.Biz;
import com.viweb.storemanager.entity.Employee;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author qhvic
 */
public class LoginDlg extends javax.swing.JDialog {

    private boolean cancel = false;
    private Employee emp = null;

    /** Creates new form LoginDlg */
    public LoginDlg(Employee emp, java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.emp = emp;
        this.txtUsername.requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        btnOk = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        lblUsername = new javax.swing.JLabel();
        lblPassword = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        txtPassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Đăng nhập");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());

        btnOk.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/ok.png"))); // NOI18N
        btnOk.setText("Đồng ý");
        btnOk.setToolTipText("Đăng nhập hệ thống");
        btnOk.setMinimumSize(new java.awt.Dimension(90, 25));
        btnOk.setPreferredSize(new java.awt.Dimension(90, 25));
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(15, 5, 5, 0);
        getContentPane().add(btnOk, gridBagConstraints);

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/media_stop_red.png"))); // NOI18N
        btnCancel.setText("Hủy bỏ");
        btnCancel.setToolTipText("Hủy bỏ cửa sổ đăng nhập");
        btnCancel.setMinimumSize(new java.awt.Dimension(90, 25));
        btnCancel.setPreferredSize(new java.awt.Dimension(90, 25));
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(15, 5, 5, 5);
        getContentPane().add(btnCancel, gridBagConstraints);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin đăng nhập", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), java.awt.Color.blue)); // NOI18N
        jPanel1.setMinimumSize(new java.awt.Dimension(247, 78));
        jPanel1.setPreferredSize(new java.awt.Dimension(247, 78));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        lblUsername.setText("Tên đăng nhập:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
        jPanel1.add(lblUsername, gridBagConstraints);

        lblPassword.setText("Mật khẩu:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 2, 2, 0);
        jPanel1.add(lblPassword, gridBagConstraints);

        txtUsername.setMinimumSize(new java.awt.Dimension(150, 22));
        txtUsername.setPreferredSize(new java.awt.Dimension(150, 22));
        txtUsername.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtUsernameActionPerformed(evt);
            }
        });
        txtUsername.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtUsernameKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 2);
        jPanel1.add(txtUsername, gridBagConstraints);

        txtPassword.setMinimumSize(new java.awt.Dimension(150, 22));
        txtPassword.setPreferredSize(new java.awt.Dimension(150, 22));
        txtPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPasswordKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 2, 2);
        jPanel1.add(txtPassword, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        getContentPane().add(jPanel1, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtUsernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtUsernameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtUsernameActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        this.cancel = true;
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
        // TODO add your handling code here:
        CursorManager.setWaitCursor(this);
        this.login();
        CursorManager.setDefaultCursor(this);
    }//GEN-LAST:event_btnOkActionPerformed

    private void txtUsernameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtUsernameKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
            this.btnOk.requestFocus();
            this.login();
        }
    }//GEN-LAST:event_txtUsernameKeyPressed

    private void txtPasswordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPasswordKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
            this.btnOk.requestFocus();
            this.login();
        }
    }//GEN-LAST:event_txtPasswordKeyPressed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        this.cancel = true;
        this.dispose();
    }//GEN-LAST:event_formWindowClosing

    private void login() {
        try {
            this.emp.setUsername(this.txtUsername.getText().trim());
            this.emp.setPassword(String.valueOf(txtPassword.getPassword()).trim());
            if (!Biz.getInstance().login(emp)) {
                // TODO kiểm tra lại ý nghĩa lệnh
                // code này tạm thời dịch là : nếu login thành công thì báo : ""Đăng nhập không thành công!\n" + Biz.getInstance().getLastError()" --> cái này là lỗi chứ ???

                OptionPane.show(JOptionPane.getFrameForComponent(LoginDlg.this),
                        "Đăng nhập không thành công!\n" + Biz.getInstance().getLastError(), "Thông báo",
                        OptionPane.MsgTYPE_WARNING);
                this.txtPassword.setText("");
                this.txtPassword.requestFocus();
                this.cancel = true;
            } else {
                //nếu vậy thì đây là phần dành cho đăng nhập ko thành công???
                this.cancel = false;
                this.dispose();
            }
        } catch (Exception ex) {
            OptionPane.show(JOptionPane.getFrameForComponent(LoginDlg.this),
                        "Có lỗi trong quá trình đăng nhập", "Thông báo",
                        OptionPane.MsgTYPE_WARNING);
        }
    }

    public boolean isCancel() {
        return cancel;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOk;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
